package com.longteng.java.web.dataproducer;

import com.google.common.io.Resources;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Created by tianqing.wang
 * DATE : 2018/3/18
 * TIME : 上午12:50
 * PROJECT : java-lesssion
 * PACKAGE : com.gitee.longteng.lession.lesson2.dataproducer
 *
 * @author <a href="mailto:celeskyking@163.com">tianqing.wang</a>
 */
public class IDCardDataGenerator implements DataGenerator<String> {

    private List<String> idcardsPrefixs;


    public IDCardDataGenerator(){
        try {
            idcardsPrefixs = Resources.readLines(Resources.getResource("idcard_prefix.txt"), StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }


    @Override
    public String generate() {
        return getPrefix()+getBirthDay()+getEnd();
    }


    private String getPrefix(){
        return idcardsPrefixs.get(RandomUtil.getRandom(0,idcardsPrefixs.size()-1))+RandomUtil.getRandom(100,999);
    }


    private String getEnd(){
        return String.valueOf(RandomUtil.getRandom(1000,9999));
    }


    private String getBirthDay(){
        int year = RandomUtil.getRandom(1900,2018);
        int month = RandomUtil.getRandom(1,12);
        int days = RandomUtil.getRandom(1,27);
        return year+monthToString(month)+dayToString(days);
    }

    private String monthToString(int month){
        if(month<10){
            return "0"+month;
        }
        return String.valueOf(month);
    }

    private String dayToString(int day){
        if(day<10){
            return "0"+day;
        }
        return String.valueOf(day);
    }

    public static void main(String[] args) {
        IDCardDataGenerator idCardDataGenerator = new IDCardDataGenerator();
        for(int i=0;i<100;i++){
            String line = idCardDataGenerator.generate();
            System.out.println("lenght:"+line.length());
            System.out.println(line);
        }
    }
}
